package main.social.dao.impl;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.Subqueries;
import org.springframework.orm.hibernate3.HibernateCallback;

import main.social.dao.EntryDao;
import main.social.entities.Entrygroup;
import main.social.entities.Entrys;
import main.social.entities.Users;

public class EntryDaoImpl extends AbstractGenericDao<Entrys, Long> implements
		EntryDao {

	@SuppressWarnings("rawtypes")
	@Override
	public List getWeeklyMostView(final int number) {
		List results = null;
		results = getHibernateTemplate().executeFind(new HibernateCallback() {
			
			@Override
			public List doInHibernate(Session session) throws HibernateException,
					SQLException {
				Criteria crit = session.createCriteria(getPersistentClass());
				crit.addOrder(Order.desc("view"));
				crit.setMaxResults(number);
				return crit.list();
			}
		});
		return results;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public List getWeeklyMostViewByUser(final Users user, final int number) {
		// TODO Auto-generated method stub
		return null;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public List getNewestEntrys(final int number) {
		List results = null;
		results = getHibernateTemplate().executeFind(new HibernateCallback() {
			
			@Override
			public List doInHibernate(Session session) throws HibernateException,
					SQLException {
				Criteria crit = session.createCriteria(getPersistentClass());
				crit.addOrder(Order.desc("date"));
				crit.setMaxResults(number);
				return crit.list();
			}
		});
		return results;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public List getNewestEntrysByUser(final Users user, final int number) {
		// TODO Auto-generated method stub
		return null;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public List getMostViewByCatalog(final String catalog, final int number) {
		List results = null;
		results = getHibernateTemplate().executeFind(new HibernateCallback() {
			
			@Override
			public List doInHibernate(Session session) throws HibernateException,
					SQLException {
				List list = null;
				DetachedCriteria entryGroup  = DetachedCriteria.forClass(Entrygroup.class);
				entryGroup.setProjection(Projections.property("id"));
				entryGroup.add(Restrictions.eq("name", catalog));
				list = session
						.createCriteria(getPersistentClass())
						.add(Subqueries.propertyEq("entryGroup", entryGroup))
						.addOrder(Order.desc("view"))
						.setMaxResults(number)
						.list();
				return list;
			}
		});
		return results;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public List getUserEntrysMostViewByCatalog(final Users user, final String catalog,
			final int number) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Map<String, Object> getUserEntrysGroupByCatalog(final Users user) {
		// TODO Auto-generated method stub
		return null;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public List getUserEntrysByCatalog(final Users user, final String catalog) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Entrys newEntityInstance() {
		Entrys result = new Entrys();
		result.setDate(new Date());
		result.setParent(null);
		result.setView(0L);
		return result;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public Entrys getEntryByUuid(final String uuid) {
		Entrys result = null;
		List list = getHibernateTemplate().executeFind(new HibernateCallback() {
			
			@Override
			public List doInHibernate(Session session) throws HibernateException,
					SQLException {
				Criteria crit = session.createCriteria(getPersistentClass());
				crit.add(Restrictions.eq("uuid", uuid));
				return crit.list();
			}
		});
		if((list != null) && (list.size() > 0)){
			result = (Entrys) list.get(0);
		}
		return result;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<Entrys> getEntrysChildByUuid(String uuid) {
		final Entrys parent = this.getEntryByUuid(uuid);
		return (List<Entrys>)getHibernateTemplate().executeFind(new HibernateCallback() {
			@Override
			public List doInHibernate(Session session) throws HibernateException,
					SQLException {
				Criteria crit = session.createCriteria(getPersistentClass());
				crit.add(Restrictions.eq("parent", parent));
				crit.addOrder(Order.asc("date"));
				return crit.list();
			}
		});
	}

	@Override
	public Entrys increaseView(final String uuid) {
		Entrys entry = this.getEntryByUuid(uuid);
		if(entry != null){
			entry.setView(entry.getView()+1);
		}
		return entry;
	}
}
